package com.sec.android.app.myfiles.widget.tablet.bottomdetailview;

import android.content.Context;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.samsung.android.sdk.slinkcloud.CloudGatewaySignInUtils;
import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.dialog.UnableToAccessServerDialogFragment;
import com.sec.android.app.myfiles.feature.MouseKeyboardMgr;
import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.navigation.NavigationInfo;
import com.sec.android.app.myfiles.navigation.NavigationManager;
import com.sec.android.app.myfiles.util.UiUtils;

import java.util.ArrayList;

/**
 * Created by yunju.cha on 2016-12-07.
 */
public class BottomDetailBTView extends BottomDetailView {
    private RelativeLayout mFileOperationLayout;
    private TextView mCancel;
    private TextView mPasteHere;
    private TextView mFileOperationInfo;

    BottomDetailBTView(View parent, Context context) {
        super(parent, context);
    }

    @Override
    public void initView(View parent) {
        super.initView(parent);
        mFileOperationLayout = (RelativeLayout) parent.findViewById(R.id.file_operation_layout);
        mFileOperationInfo = (TextView) parent.findViewById(R.id.file_operation_info);
        mCancel = (TextView) parent.findViewById(R.id.infor_bar_left_btn);
        mPasteHere = (TextView) parent.findViewById(R.id.infor_bar_right_btn);

        setBtKeyboardListener();
    }

    @Override
    public void updateView(ArrayList<FileRecord> recordList, AbsMyFilesFragment fragment) {
        clearView();
        mRecordList = new ArrayList<>(recordList);
        mFragment = fragment;

        if (MouseKeyboardMgr.isCtrlCopied()) {
            setViewInfo(mFileOperationInfo, UiUtils.makeCopiedItemsString(mContext, mRecordList));
        } else if (MouseKeyboardMgr.isCtrlMoved()) {
            setViewInfo(mFileOperationInfo, UiUtils.makeMovedItemsString(mContext, mRecordList));
        }
        updatePasteButton();
    }

    @Override
    protected void clearView() {
        super.clearView();
        setViewInfo(mFileOperationInfo, null);
    }

    @Override
    protected void setVisibility(int visibility) {
        mFileOperationLayout.setVisibility(visibility);
    }

    private void setBtKeyboardListener() {
        mPasteHere.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int processId = mFragment.getProcessId();
                NavigationInfo curInfo = NavigationManager.getInstance(processId).getCurInfo();
                if (curInfo != null) {
                    if (curInfo.getCurRecord().getStorageType() == FileRecord.StorageType.Cloud) {
                        if (!UiUtils.isNetworkOn(mContext)) {
                            UnableToAccessServerDialogFragment accessDialogFragment = UnableToAccessServerDialogFragment.getInstance(R.string.unable_to_connect_network, mFragment.getActivity());
                            accessDialogFragment.showAllowingStateLoss(mFragment.getFragmentManager(), UnableToAccessServerDialogFragment.NETWORK_WARNING);
                            return;
                        }

                        boolean isWifiOnlyMode = CloudGatewaySignInUtils.getInstance(mContext).getWifiOnlyMode();
                        boolean isWifiOn = UiUtils.isWifiOn(mContext);

                        if (isWifiOnlyMode && !isWifiOn) {
                            UnableToAccessServerDialogFragment netDialog = UnableToAccessServerDialogFragment.getInstance(R.string.unable_to_connect_wifi, mFragment.getActivity());
                            netDialog.showAllowingStateLoss(mFragment.getFragmentManager(), UnableToAccessServerDialogFragment.WIFI_WARNING);
                            return;
                        }
                    }
                    MouseKeyboardMgr.onCtrlV(mFragment, curInfo.getCurRecord());
                }
            }
        });
        mCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MouseKeyboardMgr.onCancel();
            }
        });
    }

    private void updatePasteButton() {
        boolean enabled = false;
        NavigationInfo info = mFragment.getNavigationInfo();
        if (info != null && !info.isEmptySdCardInfo() && !mFragment.getLoadingStatus() &&
                (info.getStorageType() == FileRecord.StorageType.Local || info.getStorageType() == FileRecord.StorageType.Cloud)) {
            enabled = true;
        }
        mPasteHere.setEnabled(enabled);
    }

    @Override
    public void refreshChildView() {
        setTextSize(mFileOperationInfo, R.dimen.bottom_detail_text_size);
        setTextSize(mCancel, R.dimen.action_bar_two_btn_text_size);
        setTextSize(mPasteHere, R.dimen.action_bar_two_btn_text_size);
    }
}
